package com.android.kuai.zhuan;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

import com.android.kuai.zhuan.bean.UpgradeBean;
import com.android.kuai.zhuan.bean.UserIdBean;
import com.android.kuai.zhuan.retrofit.BaseApi;
import com.android.kuai.zhuan.retrofit.HttpCommonInterceptor;
import com.android.kuai.zhuan.retrofit.api.ApiInterface;
import com.android.kuai.zhuan.service.MainService;
import com.android.kuai.zhuan.utils.AlarmManagerUtils;
import com.android.kuai.zhuan.utils.CommonUtils;
import com.android.kuai.zhuan.utils.HmSharedPreferencesUtils;
import com.android.kuai.zhuan.utils.ScreenUtils;
import com.android.kuai.zhuan.widget.DragFloatActionButton;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.internal.cache.CacheInterceptor;
import okhttp3.logging.HttpLoggingInterceptor;

public class MyApplication extends Application {

    public static Context context;
    public static String basePath;

    private static final int TIMEOUT_READ = 15;
    private static final int TIMEOUT_CONNECTION = 15;
    private static OkHttpClient mOkHttpClient;
    private static MyApplication mApplication;
    public static final Gson mGson = new Gson();
    public static boolean isRunning = false;
    private AlarmManagerUtils alarmManagerUtils;

    @Override
    public void onCreate() {
        super.onCreate();
        //CrashHandler.getInstance().init(this);
        context = this;
        mApplication = this;
        //  /data/user/0/com.android.kuai.zhuan/files
        //basePath = getFilesDir().toString();
        // /storage/emulated/0
        //basePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        // /data
        //basePath = Environment.getDataDirectory().getAbsolutePath();

        // /storage/emulated/0/Android/data/com.android.kuai.zhuan/files
        basePath = getExternalFilesDir("").getAbsolutePath();
        startDaemon();
        //防止重启不自动开始阅读
        //startService(new Intent(this, MainService.class));
        alarmManagerUtils = AlarmManagerUtils.getInstance(this);
        alarmManagerUtils.createGetUpAlarmManager();
        alarmManagerUtils.getUpAlarmManagerStartWork();
    }

    public void startDaemon() {
        try {
            Intent intent = new Intent();
            intent.setClassName("com.kuai.zhuan.daemon", "com.kuai.zhuan.daemon.MainService");
            startService(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showFAB(Context context) {
        DragFloatActionButton floatBtn = new DragFloatActionButton(context, null);
        //DragFAB floatBtn = new DragFAB(this);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        // 设置LayoutParams(全局变量）相关参数
        WindowManager.LayoutParams windowManagerParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        /**
         * 注意，flag的值可以为：
         * 下面的flags属性的效果形同“锁定”。
         * 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
         * LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件
         * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
         * LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸
         */
        // 调整悬浮窗口至左上角，便于调整坐标
        windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;
        // 以屏幕左上角为原点，设置x、y初始值
        windowManagerParams.x = ScreenUtils.getScreenWidth(context) - floatBtn.getOriginWidth();
        windowManagerParams.y = ScreenUtils.getScreenHeight(context) / 2;
        // 设置悬浮窗口长宽数据
        floatBtn.measure(0, 0);
        floatBtn.setOriginWidth(floatBtn.getMeasuredWidth());
        windowManagerParams.width = floatBtn.getOriginWidth();
        windowManagerParams.height = windowManagerParams.width;
        // 显示myFloatView图像
        windowManager.addView(floatBtn, windowManagerParams);
    }

    public OkHttpClient genericClient() {

        if (mOkHttpClient != null)
            return mOkHttpClient;

        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("leihuiming", "retrofit back  = " + message);
            }
        });
        HttpLoggingInterceptor.Level level = BuildConfig.DEBUG ?
                HttpLoggingInterceptor.Level.HEADERS :
                HttpLoggingInterceptor.Level.NONE;
        logInterceptor.setLevel(level);
        // 添加公共参数拦截器
        HttpCommonInterceptor commonInterceptor = new HttpCommonInterceptor.Builder()
                .addHeaderParams("info", "a20180101brush")
                .addHeaderParams("ip", "10.0.2.12")
                .addHeaderParams("mfrs", "apple")
                .addHeaderParams("model", "xiaomiNote2")
                .addHeaderParams("gps", "123.4545,60.46545")
                .addHeaderParams("idfa", "12345678")
                .addHeaderParams("idfv", "2312345")
                .build();


        return mOkHttpClient = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                //.addInterceptor(commonInterceptor)
                .addInterceptor(logInterceptor)
                .readTimeout(TIMEOUT_READ, TimeUnit.SECONDS)
                .connectTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)
                .build();
    }

    public static MyApplication getApplication() {
        return mApplication;
    }


}
